import pyaudio
import whisper
import numpy as np
from scipy.io.wavfile import write

# 初始化模型
model = whisper.load_model("small")

# 设置录音参数
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
CHUNK = int(RATE * 5)  # 每5秒处理一次

p = pyaudio.PyAudio()
stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)

print("开始录音并实时转录...")

while True:
    data = stream.read(CHUNK)
    audio_np = np.frombuffer(data, dtype=np.int16).astype(np.float32) / 32768.0
    result = model.transcribe(audio_np, fp16=False)
    print(result["text"])
